package main
import (
	"fmt"
	// "studygolang/31/errors"
	"math"
)
func circleArea(radius float64) (float64, error) {
	if radius < 0 {
		return 0, fmt.Errorf("Area calculation failed, radius %0.2f is less than zero", radius)
	}
	return math.Pi * radius * radius, nil
}
// 使用结构体类型和字段提供错误的更多信息
type areaError struct {
	err string
	radius float64
}
func (e *areaError) Error() string {
	return fmt.Sprintf("radius %0.2f: %s", e.radius, e.err)
}
func circleArea1(radius float64) (float64, error) {  
    if radius < 0 {
        return 0, &areaError{"radius is negative", radius}
    }
    return math.Pi * radius * radius, nil
}


//  31. 自定义错误
func main(){
	radius := -20.1
	area, err := circleArea(radius)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Area of circle %0.2f", area)
	fmt.Println("err= ", err)

}
